package io.yuanqi.hgtcshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.yuanqi.hgtcshop.dao.MemberAddressMapper;
import io.yuanqi.hgtcshop.entity.dos.MemberAddress;
import io.yuanqi.hgtcshop.entity.dos.Region;
import io.yuanqi.hgtcshop.entity.vo.PageVO;
import io.yuanqi.hgtcshop.service.MemberAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 收货地址业务层实现
 *
 * @author Chopper
 * @since 2020/11/18 9:44 上午
 */
@Slf4j
@Service
public class MemberAddressServiceImpl extends ServiceImpl<MemberAddressMapper, MemberAddress> implements MemberAddressService {

    @Autowired
    private MemberAddressMapper memberAddressMapper;

    @Override
    public List<MemberAddress> getAddressByMember(String memberId) {
        LambdaQueryWrapper<MemberAddress> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MemberAddress::getMemberId, memberId);
        List<MemberAddress> memberAddresses = memberAddressMapper.selectList(lambdaQueryWrapper);
        return memberAddresses;
    }

    @Override
    public MemberAddress getMemberAddress(String id) {
        return this.getOne(
                new QueryWrapper<MemberAddress>()
                        .eq("id", id));
    }

    /**
     * 根据地址ID获取当前会员地址信息
     *
     * @return 当前会员的地址信息
     */
    @Override
    public MemberAddress getDefaultMemberAddress(String openId) {
        return this.getOne(
                new QueryWrapper<MemberAddress>()
                        .eq("member_id", openId)
                        .eq("is_default", true));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MemberAddress saveMemberAddress(MemberAddress memberAddress) {
        //判断当前地址是否为默认地址，如果为默认需要将其他的地址修改为非默认
        removeDefaultAddress(memberAddress);
        memberAddress.setDeleteFlag(false);
        memberAddress.setCreateTime(new Date());
        memberAddress.setUpdateTime(new Date());
        memberAddress.setCreateBy(memberAddress.getMemberId());
        memberAddress.setUpdateBy(memberAddress.getMemberId());
        memberAddress.setId(String.valueOf(System.currentTimeMillis()));
        //添加会员地址
        this.save(memberAddress);
        return memberAddress;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MemberAddress updateMemberAddress(MemberAddress memberAddress) {
        MemberAddress originalMemberAddress = this.getMemberAddress(memberAddress.getId());
        if (originalMemberAddress != null) {
            if (memberAddress.getIsDefault() == null) {
                memberAddress.setIsDefault(false);
            }
            //判断当前地址是否为默认地址，如果为默认需要将其他的地址修改为非默认
            memberAddress.setMemberId(originalMemberAddress.getMemberId());
            removeDefaultAddress(memberAddress);
            if (memberAddress.getName()!=null){
                originalMemberAddress.setName(memberAddress.getName());
            }
            if (memberAddress.getMobile()!=null){
                originalMemberAddress.setMobile(memberAddress.getMobile());
            }
            if (memberAddress.getConsigneeAddressIdPath()!=null){
                originalMemberAddress.setConsigneeAddressIdPath(memberAddress.getConsigneeAddressIdPath());
            }
            if (memberAddress.getConsigneeAddressPath()!=null){
                originalMemberAddress.setConsigneeAddressPath(memberAddress.getConsigneeAddressPath());
            }
            if (memberAddress.getDetail()!=null){
                originalMemberAddress.setDetail(memberAddress.getDetail());
            }
            if (memberAddress.getIsDefault()!=null){
                originalMemberAddress.setIsDefault(memberAddress.getIsDefault());
            }
            originalMemberAddress.setUpdateTime(new Date());
            this.saveOrUpdate(originalMemberAddress);
        }
        return originalMemberAddress;
    }

    @Override
    public boolean removeMemberAddress(String id) {
        return this.remove(new QueryWrapper<MemberAddress>()
                .eq("id", id));
    }

    /**
     * 修改会员默认收件地址
     *
     * @param memberAddress 收件地址
     */
    private void removeDefaultAddress(MemberAddress memberAddress) {
        //如果不是默认地址不需要处理
        if (Boolean.TRUE.equals(memberAddress.getIsDefault())) {
            //将会员的地址修改为非默认地址
            LambdaUpdateWrapper<MemberAddress> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
            lambdaUpdateWrapper.set(MemberAddress::getIsDefault, false);
            lambdaUpdateWrapper.eq(MemberAddress::getMemberId, memberAddress.getMemberId());
            this.update(lambdaUpdateWrapper);
        }

    }
}